import {
  InsertData, DeleteData, UpdateData, SearchData,
  InsertDataSync, DeleteDataSync, UpdateDataSync, SearchDataSync,
  SelectDatabase,
} from '@/utils/db'
import {
  GetDictionaryItemName,
  GetDictionaryItemNameByArray,
  GetDictionaryArrayByValue
} from '@/utils/system'

/**
 * 添加/编辑项目
 * @param request
 * @param response
 */
const insertProject = (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '提交失败',
    data: [],
    total: -1,
  }

  let { id } = queryParams
  const _db = SelectDatabase('bug-manager', 'project', 'ProjectList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  if (id) {
    const updateDataCallback = (resJSON: any) => {
      UpdateData(_db, { id }, queryParams, resJSON, sendData)
    }
    SearchData(_db, { id }, resJSON, updateDataCallback)
  } else {
    InsertData(_db, queryParams, resJSON, sendData)
  }
}

/**
 * 获取项目列表
 * @param queryParams
 * @param resolve
 */
const getProjectList = async (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('bug-manager', 'project', 'ProjectList')

  let _queryParams: any = {}
  if (queryParams.ids.length > 0) {
    _queryParams.id = { $in: queryParams.ids }
  }
  if (queryParams.startDate) {
    _queryParams.startDate = { $gte: queryParams.startDate }
  }
  if (queryParams.endDate) {
    _queryParams.endDate = { $lte: queryParams.endDate }
  }

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  SearchData(_db, _queryParams, resJSON, sendData)
}

/**
 * 获取项目详情
 * @param queryParams
 * @param resolve
 */
const getProjectItem = (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('bug-manager', 'project', 'ProjectList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  SearchData(_db, queryParams, resJSON, sendData)
}

/**
 * 删除项目
 * @param queryParams
 * @param resolve
 */
const deleteProject = (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '删除失败',
    data: [],
    total: -1,
  }

  let _queryParams: any = {}
  if (queryParams.ids.length > 0) {
    _queryParams.id = { $in: queryParams.ids }
  }

  const _db = SelectDatabase('bug-manager', 'project', 'ProjectList')
  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }
  DeleteData(_db, _queryParams, resJSON, sendData)
}


/**
 * 添加模块
 * @param queryParams
 * @param resolve
 */
const insertModule = (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '提交失败',
    data: [],
    total: -1,
  }

  let { id } = queryParams
  const _db = SelectDatabase('bug-manager', 'project', 'ModuleList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  if (id) {
    const updateDataCallback = (resJSON: any) => {
      UpdateData(_db, { id }, queryParams, resJSON, sendData)
    }
    SearchData(_db, { id }, resJSON, updateDataCallback)
  } else {
    InsertData(_db, queryParams, resJSON, sendData)
  }
}

/**
 * 获取模块列表
 * @param queryParams
 * @param resolve
 */
const getModuleList = async (queryParams: any, resolve: Function) => {
  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('bug-manager', 'project', 'ModuleList')

  let _queryParams: any = {
    ...queryParams
  }
  if (queryParams.ids?.length > 0) {
    _queryParams["projectName.0.value"] = { $in: queryParams.ids }
  }
  if (queryParams.moduleLeader?.length > 0) {
    _queryParams["moduleLeader.0.value"] = { $in: queryParams.moduleLeader }
  }

  delete _queryParams.ids
  delete _queryParams.moduleLeader

  console.log('_queryParams', _queryParams)

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  SearchData(_db, _queryParams, resJSON, sendData)
}

const getModuleItem = (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '查询失败',
    data: [],
    total: -1,
  }

  const _db = SelectDatabase('bug-manager', 'project', 'ModuleList')

  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }

  SearchData(_db, queryParams, resJSON, sendData)
}

/**
 * 删除模块
 * @param queryParams
 * @param resolve
 */
const deleteModule = (queryParams: any, resolve: Function) => {

  let resJSON: ResponseDataType = {
    code: -1,
    note: '删除失败',
    data: [],
    total: -1,
  }

  let _queryParams: any = {}
  if (queryParams.ids?.length > 0) {
    _queryParams.id = { $in: queryParams.ids }
  }

  const _db = SelectDatabase('bug-manager', 'project', 'ModuleList')
  const sendData = (data: ResponseDataType) => {
    resolve(data)
  }
  DeleteData(_db, _queryParams, resJSON, sendData)
}


export default [
  {
    path: '/bugManager/project/insertProject',
    func: insertProject
  },
  {
    path: '/bugManager/project/getProjectList',
    func: getProjectList
  },
  {
    path: '/bugManager/project/getProjectItem',
    func: getProjectItem
  },
  {
    path: '/bugManager/project/deleteProject',
    func: deleteProject
  },
  {
    path: '/bugManager/project/insertModule',
    func: insertModule
  },
  {
    path: '/bugManager/project/getModuleList',
    func: getModuleList
  },
  {
    path: '/bugManager/project/getModuleItem',
    func: getModuleItem
  },
  {
    path: '/bugManager/project/deleteModule',
    func: deleteModule
  },
]